package com.sh169.action.admin;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.nutz.dao.Dao;
import org.nutz.dao.pager.Pager;
import org.nutz.ioc.annotation.InjectName;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Files;
import org.nutz.lang.Strings;
import org.nutz.lang.random.StringGenerator;
import org.nutz.mvc.annotation.AdaptBy;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.nutz.mvc.upload.UploadAdaptor;

import com.sh169.beans.PageForm;
import com.sh169.pojos.Article;
import com.sh169.pojos.NewsImage;
import com.sh169.pojos.Pinpai;
import com.sh169.util.ApplicationUtil;
@At("/admin")
@IocBean(name="articleAction")
@InjectName("articleAction")
public class ArticleAction {

	@Inject("refer:dao")
	private Dao dao;
	public void setDao(Dao dao){
		this.dao = dao;
	}
	@At("/article/manage")
	@Ok("tiles:admin.article.manage")
	public PageForm<Article> articleManage(@Param("pageNum")int pageNum){
		PageForm<Article> pf = new PageForm<Article>();
		Pager page = dao.createPager(pageNum<1?1:pageNum, 20);
		List<Article> arts = dao.query(Article.class, null, page);
		page.setRecordCount(dao.count(Article.class));
		pf.setResultList(arts);
		pf.setPage(page);
		return pf;
	}
	@At({"/article/preadd","/article/preupdate"})
	@Ok("tiles:admin.article.preupdate")
	public Article articlePreupdate(@Param("articleid")int articleid){
		if(articleid>0){
			Article art =dao.fetch(Article.class,articleid);
			return art;
		}
		return null;
	}
	@At({"/article/update","/article/save"})
	@Ok("redirect:/admin/article/manage")
	public void articleUpdate(@Param("::article.")Article art){
		if(art.getId()==0){
			art.setCreateTime(new Date());
			dao.insert(art);
		}else{
				art.setCreateTime(new Date());
				dao.update(art);
		}
	}
	@At("/article/delete")
	@Ok("redirect:/admin/article/manage")
	public void articleDelete(@Param("ids")int[] ids){
		if(ids!=null && ids.length>0){
			for(int id : ids){
				dao.delete(Article.class,id);
			}
		}
	}
	@At("/newsimage/manage")
	@Ok("tiles:admin.newsimage.manage")
	public List<NewsImage> newsImageManage(){
		List<NewsImage> nis = dao.query(NewsImage.class, null, null);
		if(nis.size()==0) return null;
		return nis;
	}
	@At("/newsimage/preupdate")
	@Ok("tiles:admin.newsimage.preupdate")
	public NewsImage newsImagePreupdate(@Param("id")int id){
		if(id<1)return null;
		return dao.fetch(NewsImage.class,id);
	}
	@At("/newsimage/update")
	@Ok("redirect:/admin/newsimage/manage.do")
	@AdaptBy(type=UploadAdaptor.class,args={"E:/TomcatShare/temp"})
	public void newsImageUpdate(@Param("::o.")NewsImage ni,@Param("image")File image) throws IOException{
		if(image!=null && !Strings.isEmpty(Files.getSuffixName(image))){
			if(!Strings.isEmpty(ni.getImage())){
				Files.deleteFile(new File(ApplicationUtil.getWebHomePath()+ni.getImage()));
			}
			String imagepath = "/images/newsimage/"+new StringGenerator(32, 32).next()+"."+Files.getSuffixName(image);
			Files.copyFile(image, new File(ApplicationUtil.getWebHomePath()+imagepath));
			ni.setImage(imagepath);
		}
		if(Strings.isEmpty(ni.getTitle())&&ni.getArticleid()>0){
			Article a = dao.fetch(Article.class,ni.getArticleid());
			if(a!=null){
				ni.setTitle(a.getTitle());
			}
		}
		if(ni.getId()>0){
			dao.update(ni);
		}else{
			dao.insert(ni);
		}
	}
	@At("/newsimage/delete")
	@Ok("redirect:/admin/newsimage/manage")
	public void newsImageDelete(@Param("ids")int[] ids){
		if(ids!=null && ids.length>0){
			for(int id: ids){
			dao.delete(NewsImage.class,id);
			}
		}
	}
	@At("/pinpai/manage")
	@Ok("tiles:admin.pinpai.manage")
	public List<Pinpai> pinpaiManage(){
		List<Pinpai> pps = dao.query(Pinpai.class, null, null);
		if(pps.size()==0) return null;
		return pps;
	}
	@At("/pinpai/preupdate")
	@Ok("tiles:admin.pinpai.preupdate")
	public Pinpai pinpaiPreupdate(@Param("id")int id){
		if(id>0){
			return dao.fetch(Pinpai.class,id);
		}
		return null;
	}
	@At("/pinpai/update")
	@Ok("redirect:/admin/pinpai/manage")
	@AdaptBy(type=UploadAdaptor.class,args={"E:/TomcatShare/temp"})
	public void pinpaiUpdate(@Param("::o.")Pinpai pp,@Param("image")File image) throws IOException{
		if(image!=null && !Strings.isEmpty(Files.getSuffixName(image))){
			if(!Strings.isEmpty(pp.getLogo())){
				Files.deleteFile(new File(ApplicationUtil.getWebHomePath()+pp.getLogo()));
			}
			String imagepath = "/images/logo/"+new StringGenerator(32, 32).next()+"."+Files.getSuffixName(image);
			Files.copyFile(image, new File(ApplicationUtil.getWebHomePath()+imagepath));
			pp.setLogo(imagepath);
		}
		if(pp.getId()>0){
			dao.update(pp);
		}else{
			dao.insert(pp);
		}
	}
	@At("/pinpai/delete")
	@Ok("redirect:/admin/pinpai/manage")
	public void pinpaiDelete(@Param("ids")int[] ids){
		if(ids!=null && ids.length>0){
			for(int id:ids){
				dao.delete(Pinpai.class,id);
			}
		}
	}
}
